﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyCommon
{
    /// <summary>
    /// 字符串扩展
    /// </summary>
    public static class StringExtentions
    {
        /// <summary>
        /// 转换为Int32，如果不能转换返回null
        /// </summary>
        /// <param name="input">要转换的字符串</param>
        /// <returns>转换结果</returns>
        public static int? ToInt32(this string input)
        {
            int outValue;
            if (int.TryParse(input, out outValue))
            {
                return outValue;
            }
            return null;
        }
        /// <summary>
        /// 转换为double，如果不能转换返回null
        /// </summary>
        /// <param name="input">要转换的字符串</param>
        /// <returns>转换结果</returns>
        public static double? ToDouble(this string input)
        {
            double outValue;
            if (double.TryParse(input, out outValue))
            {
                return outValue;
            }
            return null;
        }
        /// <summary>
        /// 转换为DateTime，如果不能转换返回null
        /// </summary>
        /// <param name="input">要转换的字符串</param>
        /// <returns>转换结果</returns>
        public static DateTime? ToDateTime(this string input)
        {
            DateTime outValue;
            if (DateTime.TryParse(input, out outValue))
            {
                return outValue;
            }
            return null;
        }
        /// <summary>
        /// 转换为Boolean，如果不能转换返回null
        /// </summary>
        /// <param name="input">要转换的字符串</param>
        /// <returns>转换结果</returns>
        public static bool? ToBoolean(this string input)
        {
            bool outValue;
            if (bool.TryParse(input, out outValue))
            {
                return outValue;
            }
            return null;
        }
    }
}
